package com.jzy.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jzy.weblog.common.domain.dos.UserDO;

import java.time.LocalDateTime;
import java.util.Date;

// BaseMapper表明这是一个 MyBatis-Plus 的通用 Mapper,内置函数操作数据库，不需要mapper.xml
public interface UserMapper extends BaseMapper<UserDO> {
    default UserDO findByUsername(String username) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDO::getUsername, username);
        return selectOne(wrapper); //执行查询，返回一条数据（如果存在多条数据，会抛出异常）
        // SELECT * FROM user WHERE username = #{username} LIMIT 1;
    }

    default int updatePasswordByUsername(String username, String newPassword) {
        LambdaUpdateWrapper<UserDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(UserDO::getPassword, newPassword);
        wrapper.set(UserDO::getUpdateTime, LocalDateTime.now());
        wrapper.eq(UserDO::getUsername, username);

        return update(null, wrapper);
    }
}
